
Ext.onReady(function() {
	
	$('#role_logout').click(function() {		//注销用户
		$('#role_logout').text('正在注销...');
		
		Ext.Ajax.request({
			url: '/szpxt-beta/role_logout',
			success: function(response, opts) {
				var obj = Ext.decode(response.responseText);
				if(obj.success) {
					document.location = '/szpxt-beta/main/home.jsp';
				}
			}
		});
	});
	
	new Ext.tree.TreePanel({
		id: 'extTreePanel',
		root: {id: '-1', text: '角色列表', expanded: true},
		dataUrl: '/szpxt-beta/role_getTree',
		baseCls: 'x-plain',
		lines: false,
		renderTo: 'treePanel',
		listeners: {
			click: function(node) {		
				$('.trSecond').hide();
				$('#messageRightTop').hide();
				$('#messageFalseTop').hide();
			}}
	});
	
	//取权限列表
	Ext.Ajax.request({
		url: '/szpxt-beta/role_getAllPrivilege',
		success: function(response, opts) {
			var obj = Ext.decode(response.responseText);
			listPrivilege = obj.data;
		}
	});
	
	//增删改按钮
	$('.add, .modify, .delete, .display, .submit, .clearForm, .confirm, .cancel').append('<span class="hover"></span>').each(function() {
		var $span = $('> span.hover', this).css('opacity', 0);
		$(this).hover(function() {
			$span.stop().fadeTo(500, 1);
		}, function() {
			$span.stop().fadeTo(500, 0);
		})
	});
	
	//点击事件
	$('.add, .modify').click(function() {
		$('#messageRight').hide();
		$('#messageFalse').hide();
		$('#messageRightTop').hide();
		$('#messageFalseTop').hide();
		$('#form').empty();		//先清空
		
		if(this.className == 'modify') {		//修改检查
			var selectedNode = Ext.getCmp('extTreePanel').getSelectionModel().getSelectedNode();	//获取的节点
			if(!selectedNode) {
				$('#messageFalseTop').show();
				$('#messageFalseTextTop').text('请选择需要修改的角色');
				return;
			}
		}
		
		$('#button').hide('slow');
		$('#buttonDisplay').show('slow');
		$('#bigForm').show('slow');
		
		showForm();				//后装载
		
		if(this.className == 'modify') {		//修改读取form
			loadForm();
		}
	});
	
	$('.delete').click(function() {
		var selectedNode = Ext.getCmp('extTreePanel').getSelectionModel().getSelectedNode();	//获取的节点
		if(!selectedNode) {
			$('#messageRightTop').hide();
			$('#messageFalseTop').show();
			$('#messageFalseTextTop').text('请选择需要删除的角色');
			return;
		}
		else {
			$('.trSecond').show();
		}
	});
	
	//确认删除
	$('.confirm').click(function() {
		$('#messageRightTop').hide();
		$('#messageFalseTop').hide();
		deleterole();
	})
	
	//取消删除
	$('.cancel').click(function() {
		$('#messageRightTop').hide();
		$('#messageFalseTop').hide();
		$('.trSecond').hide();
	})
	
	$('.display').click(function() {
		$('#messageRightTop').hide('slow');
		$('#messageFalseTop').hide('slow');
		$('#trSecond').hide();
		$('#buttonDisplay').hide('slow');
		$('#bigForm').hide('slow');
		
		$('#button').show('slow');
	})
	
	$('.submit').click(function() {		//提交保存
		if(Ext.getCmp('extForm').getForm().isValid()) {
			Ext.getCmp('extForm').getForm().submit({
				url: '/szpxt-beta/role_saveOrUpdate',
				success: function(form, action) {
					$('#messageFalse').hide();		//提示信息
					$('#messageRight').show();
					$('#messageRightText').text('"' + action.result.data.name + '"保存成功');
					
					Ext.getCmp('extTreePanel').root.reload();
				},
				failure: function() {
					$('#messageRight').hide();		//提示信息
					$('#messageFalse').show();
					$('#messageFalseText').text('保存失败');
				}
			});
		}
	})
	
	$('.clearForm').click(function() {
		Ext.getCmp('extForm').getForm().reset();	//清空
		Ext.getCmp('extForm').getForm().clearInvalid();
	})
	
	$('#messageRight, #messageFalse, #messageRightTop, #messageFalseTop').each(function() {
		$(this).click(function() {
			$(this).hide();
		})
	})
	
})

//显示表格
function showForm() {
	new Ext.form.FormPanel({
		id: 'extForm',
		baseCls: 'x-plain',
		width: 400,
		labelAlign: 'right',
		defaults: {xtype: 'textfield', width: 200, allowBlank: false},
		items: [
			{fieldLabel: '名称', name: 'name'},
			{xtype: 'hidden', name: 'id'},
			{xtype: 'checkboxgroup', fieldLabel: '权限列表', name: 'privilege', columns: 2, items: listPrivilege}
		],
		renderTo: 'form'
	});
}

function loadForm() {
	var selectedNode = Ext.getCmp('extTreePanel').getSelectionModel().getSelectedNode();	//获取的节点
	Ext.getCmp('extForm').getForm().load({
		url: '/szpxt-beta/role_get',
		params: {id: selectedNode.id}
	})
}

function deleterole() {
	var selectedNode = Ext.getCmp('extTreePanel').getSelectionModel().getSelectedNode();	//获取的节点
	var roleName = selectedNode.text;
	
	$('#messageRightTop').hide();
	$('#messageFalseTop').hide();
	
	Ext.Ajax.request({
		url: '/szpxt-beta/role_delete',
		params: {id: selectedNode.id},
		success: function(response, opts) {
			var obj = Ext.decode(response.responseText);
			if(obj.success) {
				$('#messageRightTop').show();
				$('#messageRightTextTop').text(roleName + '删除成功');
				$('.trSecond').hide();				
				
				Ext.getCmp('extTreePanel').root.reload();
				Ext.getCmp('extTreePanel').getSelectionModel().clearSelections();
			}
			else {
				$('#messageFalseTop').show();
				$('#messageFalseTopText').text('删除失败');
			}
		},
		failure: function(response, opts) {
			$('#messageFalseTop').show();
			$('#messageFalseTopText').text('删除失败');
		}
	})
}

